<template>
	<view class="payment ">
		<view class="header flex align-center">
			<view class="u-m-r-20">
				<image :src="logo_image" mode=""></image>
			</view>
			<view class="flex flex-column justify-evenly">
				<view class="font-30 u-m-b-20">小店名称：<text class="font-30">{{store_name}}</text></view>
				<view class="font-30">收款人：<text class="font-30">{{store_name}}</text></view>
			</view>
		</view>
		<view class="u-m-y-40 u-p-x-30 text-greey">支付金额</view>
		<view class="form border-bottom flex align-center justify-between u-m-l-30">
			<view class="flex align-center flex-1">
				<text class="font-40 ">￥</text>
				<u-input ref="input" class="flex-1 u-m-x-30" v-model="value" maxlength="10" :clearable="false"
					type="digit" placeholder="请输入金额" @input="changeValue" />
			</view>
			<view class="font-40" @tap.stop="del">×</view>
		</view>
		<view class="text-center bg-white u-m-t-80 u-p-b-30 submit">
			<dy-button width="70vw" size="default" @click="submit">确认付款</dy-button>
		</view>
	</view>
</template>

<script>
	import {SupplierCenterApi} from '@/api/serves.js' 
	// #ifdef MP-WEIXIN
	// const DGBillPlugin = requirePlugin('dg-bill');
	// #endif
	export default {
		data() {
			return {
				logo_image:'', // 商家logo
				store_name:'', // 店铺名称
				value: '',  // 输入金额
				params:{}, // 二维码参数
				
			}
		},
		onLoad(options) {
			let url = decodeURIComponent(decodeURIComponent(options.scene)) // 解码两次才能拿到正确的url
			console.log(url);
			let scene = url.split('&')
			scene.forEach(item=>{
				let o = item.split('=')
				this.params[o[0]] = o[1]
			})
			console.log(this.params);
			
			let b = uni.getStorageSync('token')
			if(!b){
				this.$Toast('请先登录')
				setTimeout(()=>{
					uni.navigateTo({
						url:'/pages/login/login/loginWeixin?isPay=1&id='+this.params.tjname
					})
				},500)
			}
			this.getMerchInfo()
		},
		methods: {
			// 获取商户信息
			async getMerchInfo(){
				let res = await SupplierCenterApi.merchDetail({id:this.params.id})
				let info = res.data.data
				this.logo_image = info.logo_image
				this.store_name = info.store_name
				console.log(res);
			},
			//清空金额
			del() {
				this.value = ''
				console.log('删除');
			},
			// 输入金额 ，防止出现第二个小数点
			changeValue(e) {
				const dotIndex = e.indexOf(".");
				const dotLastIndex = e.lastIndexOf(".");
				if (dotIndex !== dotLastIndex) {
					const p = e.slice(0, dotLastIndex);
					this.$nextTick(() => {
						this.value = p;
					});
				}
			},
			// 确认付款
			async submit() {
				let params = {
					merch_id:this.params.id,
					pay_price:this.value
				}
				if (this.value=='' || this.value<=0) {
					this.$Toast('请输入金额')
				}
				this.$ShowLoading()
				console.log(params);
				let res = await SupplierCenterApi.qrPay(params)
				console.log(res);	
				if(res.code != 1) {
					this.$HideLoading()
					this.$Toast(res.msg)
					return
				}
			    this.$HideLoading()
				// DGBillPlugin.requestPay(res.data.pre_order_id,this.paySuccess)   // 斗拱插件支付
				// wx.navigateToMiniProgram({
				//   appId: 'wx11361ccf7f47b948',//固定填写斗拱账单小程序
				//   path: '/pages/cashier/cashier?p=' + res.data.pre_order_id
				// })
				wx.requestPayment({
					...res.data,
					"prepayid": res.data.package.split('=')[1], // 统一下单订单号
					"sign": "MD5", // 签名，这里用的 MD5/RSA 签名
					success:this.paySuccess,
					fail:(err)=>{
						console.log(err)
					}
				})
			},
			paySuccess(){
				uni.switchTab({
					url:'/pages/tabbar/mine/mine'
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.payment {
		height: 100vh;
		background-color: #fff;

		.header {
			padding: 30rpx;

			image {
				width: 150rpx;
				height: 150rpx;
			}
		}

		.form {
			width: calc(100vw - 60rpx);

			.u-input__input {
				font-size: 40rpx;
			}
		}
	}
</style>